package io.github.betwowt.api.config;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author betwowt
 */
@Component
@Primary
public class NameSpaceSwaggerResourcesProvider implements SwaggerResourcesProvider {

    private static final Log log = LogFactory.getLog(NameSpaceSwaggerResourcesProvider.class);


    /**
     * 代理前的协议
     */
    public static final String PROTO = "X-Forwarded-Proto";

    /**
     * 代理前的 host
     */
    public static final String HOST = "X-Forwarded-Host";

    public static final String VERSION = "2.0";
    public static final String URL_SUFFIX = "/v2/api-docs";

    public static final String SG_IS = "swagger.includes";
    public static final String SG_BU = "swagger.baseurl";

    public static final String GA_SR = "gateway";

    private Set<String> serviceIdList;
    private String baseUrl;

    private HttpServletRequest httpServletRequest;

    public NameSpaceSwaggerResourcesProvider(Environment env,HttpServletRequest request) {
        this.httpServletRequest = request;
        Binder binder = Binder.get(env);
        serviceIdList = binder.bind(SG_IS, Bindable.setOf(String.class)).orElseGet(Collections::emptySet);
        baseUrl = binder.bind(SG_BU, Bindable.of(String.class)).orElse("");
        if (serviceIdList.isEmpty()) {
            if (log.isWarnEnabled()) {
                log.warn("未设置 swagger.includes 配置");
            }
        }
        if (log.isInfoEnabled()) {
            log.info("base url =["+baseUrl+"]");
        }
    }

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        if (CollectionUtils.isEmpty(serviceIdList)) {
            resources.add(swaggerResource("default", URL_SUFFIX, VERSION));
            return resources;
        }
        serviceIdList.stream().forEach(e -> {
            StringBuilder location = new StringBuilder();
            location.append(httpServletRequest.getHeader(PROTO))
                    .append("://")
                    .append(httpServletRequest.getHeader(HOST))
                    .append(baseUrl)
                    .append("/").append(e)
                    .append(URL_SUFFIX);
            resources.add(swaggerResource(e, location.toString(), VERSION));
        });
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setUrl(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }

}
